import { observable, action } from 'mobx';
import * as API_REQ from '../../apis/docWorkflowApi/docTemplateApi';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

export default class TemplateStore {
	@observable mouldList = [];// 模板列表
	@observable mouldid = '';// 选中模板id
	@observable mouldname = '';// 选中模板name
	@observable thurl = '';// 套红预览地址
	@observable yozoViewUrl = '';// 套红后永中预览地址
	@observable loading = true;
	@observable workflowid = 0;
	@observable selectvalue = 0;
	@observable requestid = 0;
	@observable docid = 0;
	@observable imagefileId = 0;
	@observable openyozo = false;// 永中预览
	@observable wpsEditor = false;// wps预览
	@observable error = false;
	@observable errorMsg = '';
	@observable fullView = 0;
	@observable mouldSearchValue = '';
	@observable visible = true;


	@action('初始化参数')
	doInitParams = (workflowid, requestid, docid, imagefileId, openyozo, wpsEditor) => {
		this.workflowid = workflowid;
		this.requestid = requestid;
		// this.selectvalue = selectvalue;
		this.docid = docid;
		this.imagefileId = imagefileId;
		this.openyozo = openyozo;
		this.wpsEditor = wpsEditor;
		// this.fullView = fullView;
	}

	@action('修改参数')
	setProperty = (k, v) => {
		this[k] = v;
	}

	@action
	setPropertyNew = (map) => {
		for (let i in map) {
			this[i] = map[i];
		}
	}

	@action('确定套红')
	confirmTh = () => {
		const { isColumnShow, isTextInFormCanEdit, isTextInForm, docid } = window.parent.WfForm.getOdocConfig().docParam;
		let iframeWindow;
		if (isTextInFormCanEdit && (isColumnShow == '1' || isTextInForm) && docid > 0) {
			if (isColumnShow == '1') {
				iframeWindow = document.getElementsByClassName('wf-req-form-odoc-iframe')[0].contentWindow;
			} else {
				iframeWindow = document.getElementById('requestdocInForm').contentWindow;
			}
		} else {
			iframeWindow = document.getElementsByClassName('req-workflow-odoc')[0].contentWindow;
		}
		if (this.iseditmould) {
			iframeWindow.afterSelectEditMould(undefined, { id: this.mouldid });
		} else {
			iframeWindow.afterSelectMould(undefined, { id: this.mouldid });
		}
		this.removeDialog();
	}

	@action('关闭弹框')
	removeDialog = () => {
		if (this.fullView != 1) {
			const { isColumnShow, isTextInFormCanEdit, isTextInForm, docid } = window.parent.WfForm.getOdocConfig().docParam;
			if (isTextInFormCanEdit && (isColumnShow == '1' || isTextInForm) && docid > 0) {
				if (isColumnShow == '1') {
					document.getElementsByClassName('wf-req-form-odoc-iframe')[0].style.width = '100%';
				} else {
					document.getElementById('requestdocInForm').style.width = '100%';
				}
			} else {
				document.getElementsByClassName('req-workflow-odoc')[0].style.width = '100%';
			}
			showOrHideWeboffice(1);
		}
		this.visible = false;
	}

	@action('模板查询')
	mouldSearch = (v) => {
		this.mouldSearchValue = v;
		let params = {
			workflowid: this.workflowid,
			selectvalue: this.selectvalue,
			requestId: this.requestid,
			preview: '1',
			searchValue: v,
		};
		API_REQ.getThMouldList(params).then((data) => {
			this.mouldList = data.mouldList;
		});
	}

	@action('获取模板列表')
	getMouldList = () => {
		this.loading = true;
		let params = {
			workflowid: this.workflowid,
			selectvalue: this.selectvalue,
			requestId: this.requestid,
			preview: '1',
		};
		API_REQ.getThMouldList(params).then((data) => {
			if (data.mouldList.length > 0) {
				this.mouldList = data.mouldList;
				if (data.mouldList.length > 0) {
					this.mouldid = data.mouldList[0].mouldid;
					this.mouldname = data.mouldList[0].mouldname;
					this.getThPreviewUrl();
				} else {
					this.loading = false;
				}
			} else {
				this.error = true;
				this.errorMsg = getLabel(23569, '套红模板没有设置，请和系统管理员联系！');
				this.loading = false;
			}
		});
	}

	@action('获取套红预览地址')
	getThPreviewUrl = () => {
		const { requestid, mouldid, workflowid, docid, wpsEditor, openyozo, imagefileId } = this;
		let params = { requestid, workflowid, docid };
		params.server = this.wpsEditor ? 'wps' : (this.openyozo ? 'yozo' : '0');
		params.mouldId = mouldid;
		if (wpsEditor) {
			params.paramMapStr = JSON.stringify({
				workflowid: `${workflowid}`,
				docid: `${docid}`,
				requestname: getLabel(513037, '套红预览'),
				documentTitleField: '0',
				requestid: `${requestid}`,
			});
			params.zwName = 'zw.docx';
			params.mouldType = 'showMould';
			API_REQ.getThImageFileId(params).then((data) => {
				if (data.status && data.status == true) {
					params.imagefileid = data.rtnImagefileId;
					API_REQ.getThPreviewUrl(params).then((data1) => {
						if (data1.status == '1') {
							this.thurl = data1.pdfViewUrl;
						} else {
							this.error = true;
							this.errorMsg = data1.message;
						}
						this.loading = false;
					});
				} else {
					this.error = true;
					this.errorMsg = data.message;
					this.loading = false;
				}
			}).catch(() => {
				this.error = true;
				this.errorMsg = '书签对应关系为空,请检查';
				this.loading = false;
			});
		} else if (openyozo) {
			params.imagefileid = imagefileId;
			API_REQ.getThPreviewUrl(params).then((data) => {
				if (data.status == '1') {
					this.yozoViewUrl = data.yozoViewUrl;
				} else {
					this.error = true;
					this.errorMsg = data.message;
				}
				this.loading = false;
			});
		} else {
			this.error = true;
			this.errorMsg = getLabel(515745, '参数错误，请确认是否购买金山wps或者永中预览服务');
			this.loading = false;
		}
	}
}
